#include "Client.h"

#define WINDOW_WIDTH	640
#define WINDOW_HEIGHT	480
#define WINDOW_BPP		32

HWND g_hWinHand = NULL;
HINSTANCE g_hInstance = NULL;
bool g_bExit = false;

LRESULT CALLBACK WinProc(HWND hWnd,UINT uiMsg,WPARAM wParam,LPARAM lParam)
{
	switch(uiMsg)
	{
	case WM_KEYDOWN:
		{
			break;
		}
	case WM_MOUSEMOVE:
		{
			int iMouseX = (int)LOWORD(lParam);
			int iMouseY = (int)HIWORD(lParam);
			int iButtonFlag = (int)wParam;

			switch(iButtonFlag)
			{
			case MK_LBUTTON:
				{
					break;
				}
			case  MK_RBUTTON:
				{
					break;
				}
			}
			break;
		}
	case WM_DESTROY:
		{
			g_bExit = true;
			break;
		}
	case WM_SYSKEYDOWN:
	case WM_SYSKEYUP:
		{
			if(wParam == VK_F4)
			{
				return DefWindowProc(hWnd,uiMsg,wParam,lParam);
			}
			break;
		}
	default:
		{
			return DefWindowProc(hWnd,uiMsg,wParam,lParam);
		}
	}

	return 0;
}

bool CreatWnd(HINSTANCE hInstance,int nShowCmd)
{
	TCHAR pcWindowName[128] = {0};
	swprintf(pcWindowName,TEXT("GraphicSolu"));

	WNDCLASSEX kClss;
	kClss.cbClsExtra = 0;
	kClss.cbSize = sizeof(WNDCLASSEX);
	kClss.cbWndExtra = 0;
	kClss.hbrBackground = (HBRUSH)(COLOR_WINDOWFRAME + 1);
	kClss.hCursor = LoadCursor(NULL,IDC_ARROW);
	kClss.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	kClss.hIconSm = NULL;
	kClss.hInstance = hInstance;
	kClss.lpfnWndProc = (WNDPROC)WinProc;
	kClss.lpszClassName = pcWindowName;
	kClss.lpszMenuName = pcWindowName;
	kClss.style = CS_HREDRAW | CS_VREDRAW |
				  CS_DBLCLKS | CS_OWNDC;

	if(!RegisterClassEx(&kClss))
	{
		return false;
	}

	int iWinPosX = 0;
	int iWinposY = 0;
	int iWinWidth = GetSystemMetrics(SM_CXSCREEN);
	int iWinHeight = GetSystemMetrics(SM_CYSCREEN);

	g_hWinHand = CreateWindowEx(NULL,pcWindowName,pcWindowName,WS_POPUP,iWinPosX,
		iWinposY,iWinWidth,iWinHeight,NULL,LoadMenu(hInstance,TEXT("MainMenu")),hInstance,NULL);
	if(!g_hWinHand)
	{
		return false;
	}

	g_hInstance = hInstance;

	ShowWindow(g_hWinHand,nShowCmd);
	UpdateWindow(g_hWinHand);

	return true;
}

void GameMain(void)
{
	if(!g_LuxSoul.Initialize(WINDOW_WIDTH,WINDOW_HEIGHT,WINDOW_BPP,true))
	{
		return;
	}

	MSG kMsg;
	while(true)
	{
		if(PeekMessage(&kMsg,g_hWinHand,0,0,PM_NOREMOVE))
		{
			if(!GetMessage(&kMsg,g_hWinHand,0,0))
			{
				break;
			}

			if(kMsg.message == WM_QUIT)
			{
				PostQuitMessage(0);
				break;
			}

			TranslateMessage(&kMsg);
			DispatchMessage(&kMsg);
		}

		if(!g_LuxSoul.Loop())
		{
			PostQuitMessage(0);
			break;
		}

		g_LuxSoul.Rander();

		if(g_bExit)
		{
			PostQuitMessage(0);
			break;
		}
	}

	g_LuxSoul.Finalize();
}

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	if(!CreatWnd(hInstance,nShowCmd))
	{
		return 1;
	}

	GameMain();

	return 0;
}